<?php

namespace App\Services\Game\Config;

use App\Exceptions\Response\ServiceException;
use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\Config\Game\System;

class BettingService extends Service
{
    use DatabaseMysql;

    /**
     * 获取数据列表
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Database\Eloquent\Collection[]|int[]
     */
    public function getList($request)
    {
        $query = System::query();

        // 筛选【游戏】
        $this->makeWhereParam($request, 'game_id', $query);

        // 数据条数
        $total = (clone $query)->count();
        // 排序分页
        $this->makeOrderParam($request, $query)->makePaginationParam($request, $query);
        // 查询数据
        $collection = $query->get();

        return [
            $collection,
            $total
        ];
    }


    /**
     * bet覆盖bet1
     * @return array
     */
    public function resettingBetData()
    {
        $models = System::all();

        System::dbConnection()->transaction(function () use ($models) {
            foreach ($models as $model) {
                $config = $model->config;

                if (isset($config['globals']['bet1']) || !isset($config['globals']['bet'])) {
                    continue;
                }

                $config['globals']['bet1'] = $config['globals']['bet'];

                $model->config = $config;
                $model->save();
            }
        });
    }

    /**
     * 保存配置
     *
     * @param \Illuminate\Http\Request $request
     */
    public function save($request)
    {
        // 数据 ID
        $id = $request->input('id');
        // 游戏配置
        $config = $request->input('config');
        // 游戏列表
        $gameId = $request->input('game_id');

        if (empty($id) && !empty($gameId) && System::where('game_id', $gameId)->exists()) {
            throw new ServiceException(ServiceException::SERVICE_EXISTED_DATA);
        }

        $model = System::firstOrNew([
            'game_id' => $gameId,
        ], [
            'config' => $config
        ]);
        if ($config['globals']['bet_type'] == 1){
            $config['globals']['bet'] = $config['globals']['bet1'];
        }else{
            $config['globals']['bet'] = $config['globals']['bet2'];
        }

        if ($key = $model->getKey()) {
            // 缓存配置
            $configCache = $model->config;
            // 如果是编辑配置
            if ($key == $id) {
                // 覆盖全部投注配置
                $configCache['globals']['bet1'] = $config['globals']['bet1'];
                $configCache['globals']['bet2'] = $config['globals']['bet2'];
                $configCache['globals']['bet'] = $config['globals']['bet'];
                $configCache['globals']['bet_type'] = $config['globals']['bet_type'];
            }
            // 还原配置
            $model->config = $configCache;
        }
//        dd($config);
//            dd($configCache['globals']);
        $model->save();
//            dd($configCache['globals']);
        // 通知游戏端配置修改
        $model->syncGameService();

    }

    /**
     * 使用来源游戏覆盖目标游戏投注配置
     *
     * @param string $sourceGameId
     * @param array $targetGameIds
     * @return null
     */
    public function fullGameBets(string $sourceGameId, array $targetGameIds)
    {
        // 数据源配置
        $configModel = System::where('game_id', $sourceGameId)->firstOrFail();
        // 跳过空配置
        if (empty($config = $configModel->config) || !isset($config['globals']['bet'])) {
            throw new ServiceException(ServiceException::SERVICE_NOT_EXIST_DATA);
        }
        $bet_type = !isset($config['globals']['bet_type']) ? 1 : $config['globals']['bet_type'];
        $bet2 = !isset($config['globals']['bet2']) ? [] : $config['globals']['bet2'];
        $bet1 = !isset($config['globals']['bet1']) ? [] : $config['globals']['bet1'];
        foreach ($targetGameIds as $gameId) {
            $model = System::firstOrNew([
                'game_id' => $gameId,
            ], [
                'config' => [
                    'globals' => [
                        'bet_type' => $bet_type,
                        'bet' => $config['globals']['bet'],
                        'bet1' => $bet1,
                        'bet2' => $bet2,
                    ]
                ]
            ]);

            if ($model->getKey()) {
                // 缓存配置
                $configCache = $model->config;
//                dd($configCache);
                // 覆盖全部投注配置
                $configCache['globals']['bet_type'] = $bet_type;
                $configCache['globals']['bet'] = $config['globals']['bet'];
                if ($bet_type == 2){
                    $configCache['globals']['bet2'] = $bet2;
                }
                $configCache['globals']['bet1'] = $bet1;
                // 还原配置
                $model->config = $configCache;
            }

            $model->save();

            // 通知游戏端配置修改
            $model->syncGameService();
        }
    }

    /**
     * 删除配置
     *
     * @param int $id
     */
    public function destroy($id)
    {
        $model = System::findOrFail($id);

        $config = $model->config;

        unset($config['globals']['bet']);
        unset($config['globals']['bet_type']);
        if (isset($config['globals']['line'])) {
            unset($config['globals']['line']);
        }

        if (empty($config['globals'])) {
            $config['globals'] = new \stdClass();
        }

        $model->config = $config;
        $model->save();

        // 通知游戏端配置修改
        $model->syncGameService();
    }
}
